﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bank_database_system.业务部
{
    public partial class 保险箱租用b : UserControl
    {
        private string branch_id = "";
        private string staff_id = "";
        public event EventHandler ButtonBack;
        public 保险箱租用b()
        {
            InitializeComponent();
        }

        public void branch_and_staff_get(string branchID, string staffID)
        {
            branch_id = branchID;
            staff_id = staffID;
        }

        public void CardID_Set(string text, DataTable safeBoxTable)
        {
            textBox5.Text = text;
            // 调用 SelectSafeBox 方法获取表

            // 确保表不为空并且包含数据
            if (safeBoxTable.Rows.Count > 0)
            {
                DataRow row = safeBoxTable.Rows[0];

                // 将相应的值填入 TextBox 控件
                textBox1.Text = row["SAFE_BOX_ID"].ToString();
                textBox2.Text = row["BRANCH_ID"].ToString();
                textBox3.Text = row["BOX_SIZE"].ToString();
                textBox7.Text = row["BOX_CASH_PLEDGE"].ToString();
                textBox4.Text = row["RENTAL_AMOUNT"].ToString();
                textBox6.Text = row["STATE"].ToString();
            }
            else
            {
                MessageBox.Show("没有找到相关的保险箱信息", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (textBox6.Text == "未租用")
            {
                DialogResult result = MessageBox.Show("请确认是否租用支行 " + textBox2.Text + " 的保险箱 " + textBox1.Text + " 。", "确认保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    //租用过程
                    string hostName = Dns.GetHostName(); // 获取主机名
                    IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName); // 获取本机的IP地址

                    // 过滤出IPv4地址
                    IPAddress ipv4 = ipAddresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
                    if (ipv4 == null)
                    {
                        MessageBox.Show("ip获取失败");
                        return;
                    }
                    string ip = ipv4.ToString();
                    using (输入密码 p = new 输入密码(textBox5.Text))
                    {
                        DialogResult q = p.ShowDialog();
                        if (q == DialogResult.OK)
                        {
                            decimal DateNum;
                            decimal.TryParse(textBox8.Text, out DateNum);
                            decimal CashPledge;
                            decimal.TryParse(textBox7.Text, out CashPledge);
                            decimal RentalAmount;
                            decimal.TryParse(textBox4.Text, out RentalAmount);
                            string a =
                            Safe_Box_Operations.HireSafeBox(textBox5.Text, textBox1.Text,
                                DateNum, CashPledge, RentalAmount, staff_id, ip);
                            MessageBox.Show("成功租用租用支行 " + textBox2.Text + " 的保险箱 " + textBox1.Text + " ！");
                            textBox6.Text = "被租用";//成功后更新
                        }
                        else if (q == DialogResult.No)
                        {
                            MessageBox.Show("租用失败");
                            return;
                        }
                    }
                }
            }
            else if (textBox6.Text == "租用中")
            {
                MessageBox.Show("支行 " + textBox2.Text + " 的保险箱 " + textBox1.Text + " 已被他人租用！");
            }
        }

        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            //对输入内容做限制，仅读取4位以内数字内容
            if (textBox8.TextLength == 4)
            {
                if (e.KeyChar != '\b')
                    e.Handled = true;
            }
            else
            {
                if ((e.KeyChar > '9' || e.KeyChar < '0') && e.KeyChar != '\b')
                    e.Handled = true;
            }
        }

        private void textBox8_leave(object sender, EventArgs e)
        {
            if (textBox8.Text != "")
            {
                textBox8.Text = textBox8.Text.TrimStart('0');
                if (textBox8.Text != "")
                {
                    int number = int.Parse(textBox8.Text);
                    if (number > 1000)
                    {
                        textBox8.Text = "1000";
                    }
                }
            }
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
